package com.tuweiya.study.springmvc002.eventBus.listener;

import com.google.common.eventbus.Subscribe;
import com.tuweiya.study.springmvc002.eventBus.bus.SendEventBus;
import com.tuweiya.study.springmvc002.eventBus.event.CommonEvent;
import com.tuweiya.study.springmvc002.eventBus.event.EmailEvent;
import com.tuweiya.study.springmvc002.eventBus.event.ShortMsgEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * @Author: Mr_tu
 * @Date: Create in 16:56 2018/6/7
 * @Description:
 *  事件监听者（事件订阅者Subscriber）
 *      |- 一个监听者（订阅者）可以订阅多个事件，one subscriber can subscribe more events.
 */
@Component
@Scope("singleton")
public class SendEventListener {

    private final Log logger = LogFactory.getLog(SendEventListener.class);

    @PostConstruct
    public void init(){
        logger.info("SendEventListener init()");
        SendEventBus.register(this);
        logger.error("SendEventBus.name = " + SendEventBus.identifier());
    }

    @PreDestroy
    public void destroy(){
        logger.info("SendEventListener destroy()");
        SendEventBus.unRegister(this);
    }

    // 事件处理者
    @Subscribe
    public void sendShortMsg(ShortMsgEvent shortMsgEvent){
        System.out.println("starting send Short Message");
        shortMsgEvent.sendMsg();
        System.out.println("finished send short message");
    }


    @Subscribe
    public void sendEmail(EmailEvent emailEvent){
        System.out.println("starting send email");
        emailEvent.sendEmail();
        System.out.println("finished send email");
    }

    @Subscribe
    public void lister(CommonEvent commonEvent){
        System.out.println("SendEventListener start....");
        commonEvent.something();
        System.out.println("SendEventListener end.....");
    }



}
